package com.peanut.controller;

import com.alibaba.fastjson.JSONObject;
import com.peanut.common.Result;
import com.peanut.dto.TabCarAgeDTO;
import com.peanut.server.TabCarAgeServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("dic/car/type")
@Api
public class TabCarAgeController {
    @Resource
    private TabCarAgeServer tabCarAgeServer;
    private Logger logger = LoggerFactory.getLogger(TabCarAgeController.class);

    //查询全部信息
    @ApiOperation("查询全部信息")
    @GetMapping("all")
    public Result TTabCarAge() {

        try {
            List<TabCarAgeDTO> tabCarAges = tabCarAgeServer.TabCarAge();
            logger.info("查询了全部", tabCarAges);
            String s = JSONObject.toJSONString(tabCarAges);
            List<TabCarAgeDTO> tabCarAges1 = JSONObject.parseArray(s, TabCarAgeDTO.class);
            return Result.OK().setData(tabCarAges1);
        } catch (Exception e) {
            logger.error("查询失败", e);
            return Result.ERROR();

        }
    }
    //查询全部信息
    @ApiOperation("模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam( name="carTypeName",value  ="车辆名称", paramType = "path",dataTypeClass = String.class),
    }
    )
    @PostMapping("findDicTypeCarByName/{carTypeName}")
    public Result findDicTypeCarByName(@PathVariable("carTypeName")String carTypeName)
    {
        try {
            List<TabCarAgeDTO> carTypeServices=tabCarAgeServer.findDicTypeCarByName(carTypeName);
            logger.info("查询了",carTypeServices);
            String s = JSONObject.toJSONString(carTypeServices);
            List<TabCarAgeDTO> carTypeVOS=JSONObject.parseArray(s,TabCarAgeDTO.class);
            return  Result.OK().setData(carTypeVOS);
        }
        catch (Exception e)
        {
            logger.error("查询出错",e);
            return  Result.LOGIN_ERROR();
        }
    }
    //车辆类型
    @ApiOperation("增加车辆类型")
    @ApiImplicitParams({
            @ApiImplicitParam( name="DicCarTypeVO",value  ="车辆实体类", paramType = "path",dataTypeClass = String.class),
    }
    )
    @PostMapping("addDicCarType")
    public Result addDicCarType(@RequestBody TabCarAgeDTO dicCarTypeVO)
    {
        try {
            TabCarAgeDTO tabCarAgeDTO=new TabCarAgeDTO();
            BeanUtils.copyProperties(tabCarAgeDTO,dicCarTypeVO);
            System.out.println(tabCarAgeDTO);
            logger.info("添加了",tabCarAgeDTO);
            tabCarAgeServer.addDicCarType(tabCarAgeDTO);
            return  Result.OK();
        }
        catch (Exception e)
        {
            logger.error("查询出错",e);
            return Result.LOGIN_ERROR();
        }
    }
    //车辆类型
    @ApiOperation("批量车辆类型")
    @ApiImplicitParams({
            @ApiImplicitParam( name="id",value  ="多个id", paramType = "path",dataTypeClass = String.class),
    }
    )
    @PostMapping("deleteBathByIdsCarType/{ids}")
    public Result deleteBathByIdsCarType(@PathVariable("ids") String ids)
    {
        try {
            List<Integer> list=new ArrayList<>();
            String[] split = ids.split(",");
            for (String s : split) {
                list.add(Integer.parseInt(s));
            }
            logger.info("删除了",ids);
            tabCarAgeServer.deleteBathByIdsCarType(list);
            return  Result.OK();
        }
        catch (Exception e)
        {
            logger.error("查询出错",e);
            return  Result.ERROR();
        }
    }
}
